/*****************************************************************************
 * plum_mbedtls_varint.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_cal_mbedtls_varint_encode(plum_u8 *dst, plum_u8 dstlen,
                                        plum_u64 varint)
{
    plum_s32 loop;

    for (loop = 0; varint > 0x7F; loop++) {
        if (loop > dstlen) {
            return PLUM_ECODE_INVALID;
        }

        dst[loop] = ((plum_u8)(varint & 0x7F) | 0x80);
        varint >>= 7;
    }

    dst[loop] = (plum_u8)(varint & 0x7F);

    return (loop + 1);
}

PLUM_PUBLIC
plum_s32 plum_cal_mbedtls_varint_decode(PLUM_CONST plum_u8 *src, plum_u32 ilen,
                                        plum_u64 *varint)
{
    plum_u8  byte = 0, bitpos = 0;
    plum_u64 result = 0;

    plum_s32 n = 0;

    do {
        if (bitpos >= 64) {
            return PLUM_ECODE_EPARA;
        }

        if (n >= ilen) {
            return PLUM_ECODE_INVALID;
        }

        byte = src[n++];

        result |= (plum_u64)(byte & 0x7F) << bitpos;
        bitpos += 7;
    } while (byte & 0x80);

    *varint = result;

    return (n);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
